package orxatas.travelme.sync;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import orxatas.travelme.entity.User;
import orxatas.travelme.manager.AccountManager;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;

/**
 * Opciones de configuración de las llamadas de sincronización.
 * */
public abstract class SyncOptions {
	/**
	 * Dirección URL de la API del webservice de Travelme.
	 * */
	static final protected String TRVLMWS = "http://travelme.bloodblog.net/";
	
	static final protected String TROOVEL_format = "json";
	static final protected String TROOVEL_API_KEY = "71cd4cd583942f485af72f6d88c15e3e137bf5da";
	static final protected String TROOVELWS = "http://beta.troovel.com/services/api/rest/"+TROOVEL_format+"/";
	
	static final protected String PACK_PLACE = "place";
	static final protected String OP_PLACE_DETAIL = "detail";
	
	static final protected String PACK_SEARCH = "search";
	static final protected String OP_SEARCH_MYLOCATION = "my_location";
	static final protected String OP_SEARCH_NAME = "name";
	static final protected String OP_SEARCH_LOCATION = "location";
	
	/*static final protected String TROOVEL_PACK_USER = "user";
	static final protected String TROOVEL_OP_REGISTER = "register";*/
	
	static final protected String METHOD_AM = "am";
	static final protected String ACTION_AM_LOGIN = "login";
	static final protected String ACTION_AM_REGISTER = "register";
	
	static final protected String METHOD_OM = "om";
	static final protected String ACTION_OM_ADD = "add";
	static final protected String ACTION_OM_DEL = "del";
	static final protected String ACTION_OM_LIST = "list";
	
	static final protected String METHOD_GM = "gm";
	static final protected String ACTION_GM_ADDC = "addc";
	static final protected String ACTION_GM_ADDF = "addf";
	static final protected String ACTION_GM_DELF = "delf";
	static final protected String ACTION_GM_ADDP = "addp";
	static final protected String ACTION_GM_DELP = "delp";
	static final protected String ACTION_GM_ACCEPT = "accept";
	static final protected String ACTION_GM_REJECT = "reject";
	
	static final protected String METHOD_FM = "fm";
	static final protected String ACTION_FM_ADDF = "add";
	static final protected String ACTION_FM_DELF = "del";
	static final protected String ACTION_FM_LIST = "list";
	
	static final protected String METHOD_UM = "um";
	static final protected String ACTION_UM_GET = "get";
	
	static final protected String METHOD_EM = "em";
	static final protected String ACTION_EM_ADD = "add";
	static final protected String ACTION_EM_ADDI = "addi";
	static final protected String ACTION_EM_FINISH = "finish";
	static final protected String ACTION_EM_GETPHOTO = "getp";
	static final protected String ACTION_EM_LISTE = "liste";
	
	/**
	 * Parser sencillo de json para las respuestas del servidor.
	 * */
	protected class ParseAnswer<T>{
		
		/**
		 * Parsea la respuesta dada.
		 * @param response Respuesta del servidor en formato JSON
		 * @param cls 
		 * 
		 * @return Respuesta en formato java.
		 * */
		public T parse(String response, boolean checkObj, Class<T> cls) throws WithoutObject{
			GsonBuilder builder = new GsonBuilder();
			Gson gson = builder.create();
			
			T answer = null;
			
			try {
				JSONObject json = new JSONObject(response);
				answer = gson.fromJson(json.toString(), cls);
				if(checkObj && ((Answer) answer).getObj() == null)
					throw new WithoutObject("Bad answer, without object.");
			} catch (JSONException e) {
				Log.e("JSON", "Hubo un problema extraño en la API de JSON.");
				e.printStackTrace();
				throw new WithoutObject("Bad answer, without object.");
			} catch(JsonSyntaxException e){
				Log.e("JSON", "Respuesta JSON incorrecta.");
				e.printStackTrace();
				throw new WithoutObject("Bad answer, without object.");
			}
			
			return answer;
		}
		public T parse(String response, Class<T> cls){
			GsonBuilder builder = new GsonBuilder();
			Gson gson = builder.create();
			
			T answer = null;
			
			try {
				JSONObject json = new JSONObject(response);
				answer = gson.fromJson(json.toString(), cls);
			} catch (JSONException e) {
				Log.e("JSON", "Hubo un problema extraño en la API de JSON.");
			} catch(JsonSyntaxException e){
				Log.e("JSON", "Respuesta JSON incorrecta.");
			}
			
			return answer;
		}
	}
	
	protected class WithoutObject extends Exception{
		protected WithoutObject(String msg){
			super(msg);
		}
	}
	
	
	/**
	 * Devuelve los pares básicos de una llamada al API webservice
	 * 
	 * @param method Method to call.
	 * @param action Action of the method.
	 * */
	protected List<NameValuePair> basicGETParams(AccountManager acm, String method, String action){
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		
		pairs.add(new BasicNameValuePair("method", method+"."+action));
		
		pairs.add(new BasicNameValuePair("email", acm.getUserLogged().getEmail()));
		pairs.add(new BasicNameValuePair("pass", acm.getPassword()));
		
		return pairs;
	}
	
	protected List<NameValuePair> basicGETParamsTrovel(String pack, String operation){
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		pairs.add(new BasicNameValuePair("method", pack+"."+operation));
		pairs.add(new BasicNameValuePair("api_key", TROOVEL_API_KEY));
		
		return pairs;
	}
}
